﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using jSLApp.Client.ApplicationBase;
using jSLCore.Client.Presentation.BaseControl.Selection;

namespace jSLApp.Client.Forms
{
    public partial class ArticleList : SelectControl
    {
        public ArticleList()
        {
            InitializeComponent();
        }

        private void Search()
        {
            DoWork("Поиск статей",(sender, args) =>
                                      {
                                          args.Result = Communication.Proxy.SearchArticles(args.String);
                                      },(o, eventArgs) =>
                                                            {
                                                                if(eventArgs.Error==null)
                                                                {
                                                                    grid.ItemsSource = (IEnumerable) eventArgs.Result;
                                                                }
                                                            },txt.Text);
        }

        public override void LoadInitialData()
        {
            Search();
        }


        private void create_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new NewArticle();
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   ((MainContentContainer)ParentContentContainer).GotoArticle(
                                                         (int)data);
                                   //Обновлять не надо - потому что переход на другую страницу
                                   /*InvokeCancelSelected(); че за херь, тут нет диалога*/
                               },this );
        }

        #region dialog

        public override ISelectableGrid GetSelectableGrid()
        {
            return grid;
        }

        protected override void AcceptButtonClicked()
        {
            Search();
        }

        public override Control GetControlForInitialFocus()
        {
            return txt;
        }

        #endregion

    }
}
